package me.hgj.jetpackmvvm.ledun.ui.fragment.me

import android.app.AlarmManager
import android.app.PendingIntent
import android.app.TimePickerDialog
import android.content.Intent
import android.os.Bundle
import android.telephony.PhoneNumberUtils.formatNumber
import android.text.format.DateFormat
import android.util.Log
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import kotlinx.android.synthetic.main.fragment_me.*
import me.hgj.jetpackmvvm.ledun.R
import me.hgj.jetpackmvvm.ledun.app.appViewModel
import me.hgj.jetpackmvvm.ledun.app.base.BaseFragment
import me.hgj.jetpackmvvm.ledun.app.ext.init
import me.hgj.jetpackmvvm.ledun.app.ext.joinQQGroup
import me.hgj.jetpackmvvm.ledun.app.ext.jumpByLogin
import me.hgj.jetpackmvvm.ledun.app.ext.setUiTheme
import me.hgj.jetpackmvvm.ledun.data.model.bean.BannerResponse
import me.hgj.jetpackmvvm.ledun.data.model.bean.IntegralResponse
import me.hgj.jetpackmvvm.ledun.databinding.FragmentMeBinding
import me.hgj.jetpackmvvm.ledun.viewmodel.request.RequestMeViewModel
import me.hgj.jetpackmvvm.ledun.viewmodel.state.MeViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.ext.util.alarmManager
import me.hgj.jetpackmvvm.ext.util.notNull
import me.hgj.jetpackmvvm.ledun.app.util.CacheUtil
import java.util.Calendar

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/23
 * 描述　: 我的
 */

class MeFragment : BaseFragment<MeViewModel, FragmentMeBinding>() {



    override fun initView(savedInstanceState: Bundle?) {
        //初始化
        mDatabind.includeToolbar.toolbar.run {
            init("定时光照设置")
        }
        rtime.text="${formatNumber(CacheUtil.getRiseHour())}:${formatNumber(CacheUtil.getRiseMunite())}"
        stime.text="${formatNumber(CacheUtil.getHour())}:${formatNumber(CacheUtil.getMunite())}"

        //设置日落时间
        rtSunSet.setOnClickListener {
            showTimePickerDialog(false)
        }

        //设置日出时间
        rtSunRise.setOnClickListener {

            showTimePickerDialog(true)
        }
    }
    fun formatNumber(number: Int): String {
        return if (number < 10) {
            "0$number"
        } else {
            number.toString()
        }
    }
    fun printCalendarDate(calendar: Calendar) {
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH) + 1  // Calendar.MONTH 是从0开始的
        val day = calendar.get(Calendar.DAY_OF_MONTH)
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)
        val second = calendar.get(Calendar.SECOND)
        Log.e("laiyiwen","Current Date and Time:Year: $year Month: $month Day: $day Hour: $hour Minute: $minute Second: $second")
    }
    private fun showTimePickerDialog(isRise:Boolean) {
        // 获取当前时间
        val c = Calendar.getInstance()
        val hour = c.get(Calendar.HOUR_OF_DAY)
        val minute = c.get(Calendar.MINUTE)

        // 创建一个新的时间选择器对话框
        TimePickerDialog(activity, { view, selectedHour, selectedMinute ->
            // 用户设置时间后的处理代码，例如更新UI或保存时间
            // 这里只是简单地显示一个消息
            val timeSet = "Time selected: $selectedHour:$selectedMinute"
            if (isRise){
                CacheUtil.setRiseHour(selectedHour)
                CacheUtil.setRiseMinute(selectedMinute)
                rtime.text=" $selectedHour:$selectedMinute"
                setExactAlarm("MORNING_ALARM",selectedHour,selectedMinute)

            }else{
                CacheUtil.setMunite(selectedMinute)
                CacheUtil.setHour(selectedHour)
                stime.text=" $selectedHour:$selectedMinute"
                setExactAlarm("EVENING_ALARM",selectedHour,selectedMinute)

            }
        }, hour, minute, DateFormat.is24HourFormat(activity)
        ).show()
    }

    fun setExactAlarm( action: String,hour: Int, minute: Int) {
        val intent = Intent(activity, AlarmReceiver::class.java)

        intent.action = action
        // 使用 FLAG_UPDATE_CURRENT 确保如果已经存在相同的 PendingIntent，则更新它
        val pendingIntent = PendingIntent.getBroadcast(
            activity, action.hashCode(), intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
        )
        val calendar = Calendar.getInstance()
        calendar.set(Calendar.HOUR_OF_DAY, hour)
        calendar.set(Calendar.MINUTE, minute)
        calendar.set(Calendar.SECOND, 0)
        printCalendarDate(calendar)
        // 设置精确闹钟
        activity?.alarmManager!!.setExact(
            AlarmManager.RTC_WAKEUP,
            calendar.timeInMillis,
            pendingIntent
        )
    }

    override fun lazyLoadData() {

    }

    override fun createObserver() {

    }

}